import jdk.nashorn.internal.ir.annotations.Ignore;

import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: mac
 * Date: 2022-09-11
 * Time: 16:39
 */
public class TestDemo {
    //StringBuilder
    public static void main(String[] args) {
        /*StringBuilder stringBuilder = new StringBuilder("abcdef");
        System.out.println(stringBuilder);*/
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("abcdef");//调用super.方法，不会产生新的对象
        stringBuilder.append("123456");
        System.out.println(stringBuilder);
    }
    public static void main12(String[] args) {
        //去除字符串左右两边空格
        String str1 = "    abc    defg    ";
        System.out.println(str1.trim());
        System.out.println("=============");

        //大小写转换 - 其它字符不管
        String str2 = "abcdefBFRG123";
        System.out.println(str2.toUpperCase());//小写转大写
        System.out.println(str2.toLowerCase());//大写转小写

        //字符串入字符串常量池池操作
        str2.intern();

        //拼接字符串 - 拼接好的对象不会入池
        String str = "abcdef";//""才叫空，而null表示不指向任何对象
        System.out.println(str.concat("bit"));

        //求字符串长度
        System.out.println(str.length());//方法
        int[] array = {1,2,3,4,5};
        System.out.println(array.length);//属性

        //判空
        System.out.println(str.isEmpty());
    }
    public static void main11(String[] args) {
        //字符串截取 - new了一个新对象
        String str = "abcdefgh";
        System.out.println(str.substring(2,4));//提取子串
        //java里边一般是左闭右开
    }
    public static void main10(String[] args) {
        // | 作为多个分隔符处理项
        String str = "Java30 12&21#hello";
        String[] strings = str.split(" |&|#");
        for (String s:strings
             ) {
            System.out.println(s);
        }

    }
    public static void main9(String[] args) {
        // .号为特殊字符需要/来转义，但同时/也为特殊字符故需要//.即可
        String str = "192.168.1.1";
        String[] strings = str.split("\\.",2);//limit表示分割组数，此处最多为4组
        for (String s:strings
             ) {
            System.out.println(s);
        }
        System.out.println("=============");
        String str2 = "192\\168\\1\\1";
        String[] strings1 = str2.split("\\\\",7);
        for (String s:strings1
             ) {
            System.out.println(s);
        }
    }
    public static void main8(String[] args) {
        //以某个分隔符来分割字符串 - 返回值是个String 数组
        String str = "name=zhangsan&age=19";
        String[] strings = str.split("&");
        for (String s:strings
             ) {
            String[] ss  = s.split("=");
            for (String str1:ss
                 ) {
                System.out.println(str1);
            }
        }
    }
    public static void main6(String[] args) {
        String str = "ababcabcdabcde";

        //将对应字符替换为新字符 - 假替换，产生新对象
        System.out.println(str.replace("ab", "pp"));

        //同等效果同上
        System.out.println(str.replaceAll("ab","pp"));

        //替换第一个匹配的字段
        System.out.println(str.replaceFirst("ab", "pp"));
    }
    public static void main5(String[] args) {
        String str1 = "ababcabcd";
        String tmp1 = "abc";

        //判断字段tmp是否存在于str中 - 类似于C中的strstr
        System.out.println(str1.contains(tmp1));
        System.out.println("========");

        //找到第一次出现tmp字段的位置 - 底层为KMP算法
        System.out.println(str1.indexOf(tmp1,3));

        String str2 = "ababcabcd";
        String tmp2 = "abc";

        //往前找到标记位置fromIndex- 正序匹配
        System.out.println(str2.lastIndexOf(tmp2,4));

        //判断首字段是否为所给字段
        System.out.println(str2.startsWith("c",4));

        //判断尾字段是否为所给字段
        System.out.println(str2.endsWith("cd"));
    }
    public static void main4(String[] args) {
        //不区分大小写的比较 - 返回值为int型
        String str1 = "Abcdef";
        String str2 = "abcdef";
        System.out.println(str1.equalsIgnoreCase(str2));

        //字符串比较，返回值为整数
        int ret = str1.compareTo(str2);
        System.out.println(ret);
    }
    public static void main3(String[] args) throws UnsupportedEncodingException {
        //字节数组变字符串
        byte[] bytes = {97,98,99,100};
        String str = new String(bytes,1,3);
        System.out.println(str);
        System.out.println("==========");

        //字符串变字节数组，并且可以换编码方式 utf-8
        String str2 = "张三";
        byte[] bytes1 = str2.getBytes("utf-8");
        System.out.println(Arrays.toString(bytes1));
    }
    public static boolean isNumberChar(String s){
        for (int i = 0; i < s.length(); i++) {
            char ch = s.charAt(i);
            /*if (ch < '0' || ch > '9'){
                return false;
            }*/
            if (!Character.isDigit(ch)){
                return false;
            }
        }
        return true;
    }
    public static void main2(String[] args) {
        //自己学习Character这个包 + 配置assert
        String str = "123a567";
        System.out.println(isNumberChar(str));
    }
    public static void main1(String[] args) {
        //1.将字符转变为字符串
        char[] val1 = {'a','b','c','d','e'};
        String str1 = new String(val1);
        System.out.println(str1);

        //2.指定位置转为字符串
        char[] val2 = {'a','b','c','d','e'};
        String str2 = new String(val2,1,3);
        System.out.println(str2);

        //3.获取字符串中字符
        System.out.println("========");
        String str3 = "hello";
        char ch = str3.charAt(2);//获取2下标的字符
        System.out.println(ch);

        char[] chars = str3.toCharArray();//把str3所指向的字符串对象转为字符数组
        System.out.println(Arrays.toString(chars));
    }
}
